/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2019
 */

package com.jzwl.site.manager.tradingareashopinfo.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzwl.common.id.Sequence;
import com.jzwl.common.page.PageObject;
import com.jzwl.system.base.dao.BaseDAO;

@Repository("v2tradingAreaShopInfoDao")
public class TradingAreaShopInfoDao {

	@Autowired
	private BaseDAO baseDAO;//dao基类，操作数据库

	public boolean addTradingAreaShopInfo(Map<String, Object> map) {

		//自动注入时间戳为ID 酌情修改数据库类型为bigint  int会越界
		map.put("id", Sequence.nextId());
		map.put("isDelete", 0);
		map.put("createDate", new Date());

		String sql = "insert into trading_area_shop_info "
				+ " (id,shopName,shopAdress,keyword,sort,coordinate,isDelete,createDate) " + " values "
				+ " (:id,:shopName,:shopAdress,:keyword,:sort,:coordinate,:isDelete,:createDate)";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public String getColumns() {
		return "" + " id as id," + " shopName as shopName," + " shopInfo as shopInfo," + " shopAdress as shopAdress,"
				+ " keyword as keyword," + " sort as sort," + " coordinate as coordinate," + " isDelete as isDelete,"
				+ " createDate as createDate";
	}

	public PageObject queryTradingAreaShopInfoList(Map<String, Object> map) {
		// [column]为字符串拼接, {column}为使用占位符. 如username='[username]',偷懒时可以使用字符串拼接 
		// [column] 为PageRequest的属性

		String sql = "select " + getColumns() + " from trading_area_shop_info t where 1=1 ";

		if (null != map.get("shopName") && StringUtils.isNotEmpty(map.get("shopName").toString())) {
			sql = sql + " and t.shopName  = " + map.get("shopName") + "";
		}
		if (null != map.get("shopInfo") && StringUtils.isNotEmpty(map.get("shopInfo").toString())) {
			sql = sql + " and t.shopInfo  = " + map.get("shopInfo") + "";
		}
		if (null != map.get("shopAdress") && StringUtils.isNotEmpty(map.get("shopAdress").toString())) {
			sql = sql + " and t.shopAdress  = " + map.get("shopAdress") + "";
		}
		if (null != map.get("keyword") && StringUtils.isNotEmpty(map.get("keyword").toString())) {
			sql = sql + " and t.keyword  = " + map.get("keyword") + "";
		}
		if (null != map.get("sort") && StringUtils.isNotEmpty(map.get("sort").toString())) {
			sql = sql + " and t.sort  = " + map.get("sort") + "";
		}
		if (null != map.get("coordinate") && StringUtils.isNotEmpty(map.get("coordinate").toString())) {
			sql = sql + " and t.coordinate  = " + map.get("coordinate") + "";
		}
		if (null != map.get("isDelete") && StringUtils.isNotEmpty(map.get("isDelete").toString())) {
			sql = sql + " and t.isDelete  = " + map.get("isDelete") + "";
		}
		if (null != map.get("createDate") && StringUtils.isNotEmpty(map.get("createDate").toString())) {
			sql = sql + " and t.createDate >= " + map.get("createDateBegin") + "";
			sql = sql + " and t.createDate <= " + map.get("createDateEnd") + "";
		}

		sql = sql + " order by id desc";

		PageObject po = baseDAO.queryForMPageList(sql, new Object[] {}, map);

		return po;
	}

	public List queryList(Map<String, Object> map) {
		// [column]为字符串拼接, {column}为使用占位符. 如username='[username]',偷懒时可以使用字符串拼接 
		// [column] 为PageRequest的属性

		String sql = "select " + getColumns() + " from trading_area_shop_info t where 1=1 ";
		//sql = sql + "and t.keyword like '%" + map.get("keyword") + "%'";
		List list = baseDAO.queryForList(sql);
		return list;
	}

	public boolean updateTradingAreaShopInfo(Map<String, Object> map) {

		String sql = "update trading_area_shop_info set "
				+ " id=:id,shopName=:shopName,shopAdress=:shopAdress,keyword=:keyword,sort=:sort,coordinate=:coordinate "
				+ " where id=:id";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean deleteTradingAreaShopInfo(Map<String, Object> map) {

		String sql = "delete from trading_area_shop_info where id in (" + map.get("id") + " ) ";

		return baseDAO.executeNamedCommand(sql, map);
	}

	@SuppressWarnings("unchecked")
	public Map<String, Object> getById(Map<String, Object> map) {

		Map<String, Object> resMap = new HashMap<String, Object>();

		String sql = "select " + getColumns() + " from trading_area_shop_info t where t.id = " + map.get("id") + "";

		resMap = baseDAO.queryForMap(sql);

		return resMap;

	}

}
